﻿#include "additems.h"
#include "ui_additems.h"

AddItems::AddItems(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AddItems)
{
    ui->setupUi(this);

    // 取消窗口最大化功能
    setWindowFlags(windowFlags() & ~Qt::WindowMaximizeButtonHint);
    setWindowFlags(windowFlags() & ~Qt::WindowCloseButtonHint);
    // 固定窗体大小
    setFixedSize(this->width(),this->height());

    ui->lineEdit_id->setFocus();
}

AddItems::~AddItems()
{
    delete ui;
}

void AddItems::on_exit_btn_clicked()
{
    close();
}

void AddItems::on_add_btn_clicked()
{
    QSqlQuery add_query;
    QString id,name,num,price,suppiler,director,intime,outtime,comments;

    id = ui->lineEdit_id->text();
    name = ui->lineEdit_name->text();
    num = ui->lineEdit_num->text();
    price = ui->lineEdit_price->text();
    suppiler = ui->lineEdit_suppiler->text();
    director = ui->lineEdit_director->text();

    QDateTime dateTime = QDateTime::currentDateTime();
    intime = dateTime.toString("yyyy-MM-dd HH:mm:ss");
    outtime = dateTime.toString("yyyy-MM-dd HH:mm:ss");

    comments = ui->textEdit_comment->toPlainText();

    if(ui->lineEdit_id->text().isEmpty()) {
        QMessageBox::critical(this,"Tip","The id can not be empty!!!");
        ui->lineEdit_id->setFocus();
        return;
    }

    QString sql = QString("insert into commoditydatatable"
                          "(Stockid,Stockname,Stockamount,Stockunitprice,Supplier,Director,Warehousetime,Deliverytime,comments) "
                          "values('%1','%2','%3','%4','%5','%6','%7','%8','%9')").arg(id).arg(name).arg(num).arg(price).arg(suppiler)
                          .arg(director).arg(intime).arg(outtime).arg(comments);

    if(add_query.exec(sql)) {
        QMessageBox::information(this,"Tip","Insert data successfully!");
    } else {
        QMessageBox::critical(this,"Warning","Insert data failed!");
    }
}
